package main

import (
	"fmt"
	"html/template"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
	//定义模板
	//解析模板
	t, err := template.ParseFiles("./index.tmpl")
	if err != nil {
		fmt.Printf("parse templates failed,err:%v\n\n", err)
		return
	}
	msg := "这是index页面"
	//渲染模板
	t.Execute(w, msg)
}

func home(w http.ResponseWriter, r *http.Request) {
	//定义模板
	//解析模板
	t, err := template.ParseFiles("./home.tmpl")
	if err != nil {
		fmt.Printf("parse templates failed,err:%v\n\n", err)
		return
	}
	msg := "这是home页面"
	//渲染模板
	t.Execute(w, msg)
}

func index2(w http.ResponseWriter, r *http.Request) {
	//定义模板
	//解析模板
	t, err := template.ParseFiles("./templates/base.tmpl", "./templates/index.tmpl") //有根模板时,根模板放在前面
	if err != nil {
		fmt.Printf("parse templates failed,err:%v\n\n", err)
		return
	}
	msg := "这是index页面"
	//渲染模板
	//t.Execute(w, msg)
	t.ExecuteTemplate(w, "index.tmpl", msg)
}

func home2(w http.ResponseWriter, r *http.Request) {
	//定义模板
	//解析模板
	t, err := template.ParseFiles("./templates/base.tmpl", "./templates/home.tmpl")
	if err != nil {
		fmt.Printf("parse templates failed,err:%v\n\n", err)
		return
	}
	msg := "这是home页面"
	//渲染模板
	t.Execute(w, msg)
}
func main() {
	http.HandleFunc("/index", index)
	http.HandleFunc("/home", home)
	http.HandleFunc("/index2", index2)
	http.HandleFunc("/home2", home2)
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Printf("HTTP server start failed,err:%v\n", err)
		return
	}
}
